package com.wandian.weather.mvvm.constellation;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.reflect.TypeToken;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.weather.bean.XingZuoBean;
import com.wandian.weather.bean.XingZuoDetailBean;
import com.wandian.weather.data.BaseAppApi;
import com.wandian.weather.tools.JmTools;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description ConstellationViewModel
 **/
public class ConstellationViewModel extends BaseViewModel {

    public MutableLiveData<List<XingZuoBean>>  xingZuoListBean= new MutableLiveData<>();
    public ConstellationViewModel(@NonNull Application application) {
        super(application);
    }

    public void getConstellationData(){
        //创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.tianqijun.com/") //基础url,其他部分在GetRequestInterface里
                .addConverterFactory(GsonConverterFactory.create()) //Gson数据转换器
                .build();
        //创建网络请求接口实例
        BaseAppApi request = retrofit.create(BaseAppApi.class);
        HashMap<String, String> hashMap = new HashMap<>();
        //其他参数
        //公用参数
        hashMap.put("os","1");
        hashMap.put("pid","4");
        hashMap.put("version","6.5.4");
        Log.d("getConstellationData", "error=1" );

        Call<Object> call = request.getConstellationData(JmTools.encryptParams(hashMap, "Constellation.Constella"));
        //发送网络请求(异步)
        call.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                Log.d("getConstellationData1", "getConstellationData="+ JsonUtil.toJson(response.body()));
                if(UIUtils.isEmpty(response.body())){
                    return;
                }
                String json = JsonUtil.toJson(response.body());
                if(!UIUtils.isEmpty(json)){
                    List<XingZuoBean> listBean = (List<XingZuoBean>) JsonUtil.fromJson(json, new TypeToken<ArrayList<XingZuoBean>>(){});
                    if(!UIUtils.isEmpty(listBean) && listBean.size()>0){
                        xingZuoListBean.setValue(listBean);
                    }else {
                        xingZuoListBean.setValue(null);
                    }
                }
                xingZuoListBean.setValue(null);
            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                xingZuoListBean.setValue(null);
                ToastUtil.show(t.getMessage());
            }
        });
    }

    public void getItemConstellationData(String pinyin){
        //创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.tianqijun.com/") //基础url,其他部分在GetRequestInterface里
                .addConverterFactory(GsonConverterFactory.create()) //Gson数据转换器
                .build();
        //创建网络请求接口实例
        BaseAppApi request = retrofit.create(BaseAppApi.class);
        HashMap<String, String> hashMap = new HashMap<>();
        //其他参数
        hashMap.put("pinyin",pinyin);
        //公用参数
        hashMap.put("os","1");
        hashMap.put("pid","4");
        hashMap.put("version","6.5.4");
        Log.d("getConstellationData", "error=11" );
        Call<Object> call = request.getItemConstellationData(JmTools.encryptParams(hashMap, "Constellation.Yunshi"));
        //发送网络请求(异步)
        call.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                Log.d("getItemConstellation2", "getItemConstellation="+ JsonUtil.toJson(response.body()));
                if(UIUtils.isEmpty(response.body())){
                    return;
                }
                String json = JsonUtil.toJson(response.body());
                if(!UIUtils.isEmpty(json)){
                    XingZuoDetailBean zuoDetailBean = (XingZuoDetailBean)JsonUtil.parseJsonToBean(json, XingZuoDetailBean.class);
                }
            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                Log.d("getWeatherData", "error="+t.getMessage());
            }
        });
    }


    public void getConstellationDetails(){
        //创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.tianqijun.com/") //基础url,其他部分在GetRequestInterface里
                .addConverterFactory(GsonConverterFactory.create()) //Gson数据转换器
                .build();
        //创建网络请求接口实例
        BaseAppApi request = retrofit.create(BaseAppApi.class);
        HashMap<String, String> hashMap = new HashMap<>();
        //其他参数
        hashMap.put("id","6561");
        //公用参数
        hashMap.put("os","1");
        hashMap.put("pid","4");
        hashMap.put("version","6.5.4");
        Log.d("getConstellationData", "error=11" );
        Call<Object> call = request.getConstellationDetails(JmTools.encryptParams(hashMap, "Constellation.Ysdetail"));
        //发送网络请求(异步)
        call.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                Log.d("getItemConstellation3", "getItemConstellation="+ JsonUtil.toJson(response.body()));
                if(UIUtils.isEmpty(response.body())){
                    return;
                }
                String jsonn = JsonUtil.toJson(response.body());

                try {
                    JSONObject jsonObject = new JSONObject(JsonUtil.toJson(response.body()));
//                    TianqiWeather30Bean weather30Bean =(TianqiWeather30Bean) JsonUtil.parseJsonToBean(jsonObject.optString("data"), TianqiWeather30Bean.class);
//                    if(!UIUtils.isEmpty(weather30Bean)){
//                        Log.d("getdddddd ", "1111111="+ jsonn);
//                        tianqiWeather30Bean.setValue(weather30Bean);
//                    }else {
//
//                        Log.d("getdddddd", "为空=");
//                    }
                } catch (JSONException e) {
                    Log.d("getConsteldetail", "异常=");
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                Log.d("getWeatherData", "error="+t.getMessage());
            }
        });
    }


}
